$(function() {
	var page1 = 1;
	var page2 = 1;
	var page3 = 1;

	$("#loading-first").fadeOut(500);
	ajaxuserecord()
	$('.bill').on('click', function() {
		$(this).parent().find('div').removeClass('bill_action')
		$(this).addClass('bill_action')
		if($(this).data('tabidx') == 1) {
			$('.bill_use').css('display', 'block')
			$('.bill_topup').css('display', 'none')
			$('.account').css('display', 'none')
			ajaxuserecord()
		} else if($(this).data('tabidx') == 2) {
			$('.bill_use').css('display', 'none')
			$('.bill_topup').css('display', 'block')
			$('.account').css('display', 'none')
			ajaxrechargerecord()
		} else {
			$('.bill_use').css('display', 'none')
			$('.bill_topup').css('display', 'none')
			$('.account').css('display', 'block')
			ajaxaccount()
		}

	})
	$('.bill_sm').on('click', function() {
		$(this).parent().find('div').removeClass('bill_sm_action')
		$(this).addClass('bill_sm_action')

	})

	function ajaxaccount() {
		var index = layer.load(0, {
		  shade: [0.1,'#fff'] //0.1透明度的白色背景
		});
		$.ajax({
			type: "post",
			url: "{:url('/adminapi/index/getConfig/name/check_state')}",
			success: function(res1) {
				var state = res1.data
				$.ajax({
					url: "{:url('adminapi/check/index')}",
					type: 'post',
					data: {
						company_id: $('.company_all_id').val()
					},
					success: function(res) {
						Load(1)
						console.log(res)
						var checklist = ``;
						var statemain;
						var arr11 = []
						for(var ii2 = 0; ii2 < 12; ii2++) {
							for(var key in res.data) {
								if((key.slice(5) - 0) == ii2 + 1) {
									if(res.data[key].month) {} else {
										res.data[key].month = key
									}
									arr11.push(res.data[key])
								}
							}
						}
						for(var accoidx = 0; accoidx < arr11.length; accoidx++) {
							if(arr11[accoidx].state) {
								statemain = state[arr11[accoidx].state]
							} else {
								statemain = '-'
							}

							checklist += `
								<tr data-month='${arr11[accoidx].month}'>
									<td>${arr11[accoidx].month}</td>
									<td class='ordertotal'>${arr11[accoidx].order_total?arr11[accoidx].order_total:'-'}</td>
									<td class='ordernumber'>${arr11[accoidx].order_number?arr11[accoidx].order_number:'-'}</td>
									<td class='state' data-state='${arr11[accoidx].state?arr11[accoidx].state:0}'>${statemain}</td>
								`

							var addoperation = `
									<td class='operation' data-id='${arr11[accoidx].id}'>
										<div class="tpl-table-black-operation">
											<a class="detail">
												<i class="am-icon-pencil"></i> 查看
											</a>
										</div>
									</td>`

							checklist += addoperation
							checklist += `</tr>`

						}
						$('#account1 tbody').html(checklist)
						$('.account .pagination').html(pagechang1(arr11.length, 10, page3))
						$('.account .laquo').on('click', function() {
							page -= 1;
							console.log(page)
							ajaxbuycar(page)
						})
						$('.account .pageli').on('click', function() {
							page = $(this).html()
							ajaxbuycar(page)
						})
						//下一页
						$('.account .raquo').on('click', function() {
							page += 1;
							ajaxbuycar(page)
						});

						$('.detail').on('click', function() {
							window.location.href = "{:url('account')}?id=" + $(this).parent().parent().data('id');
						})

					},
					error: function() {
						Err('请求失败')
						Load(1)
					}
				})
			},
			error: function() {
				Err('请求失败');
				Load(1)
			}
		})
	}

	function ajaxuserecord() {
		var tddata = [{
			key: 'order_time',
			type: 1,
			url: ''
		}, {
			key: 'order_no',
			type: 2,
			class: 'order',
			id: 'order_id'
		}, {
			key: 'order_money',
			type: 3,
			state: 'state' //判断数值的正负值
		}];
		var addoperation;
		var index = layer.load(0, {
		  shade: [0.1,'#fff'] //0.1透明度的白色背景
		});
		$.ajax({
			type: "post",
			url: "{:url('adminapi/Check/waste')}",
			data: {
				car_use_id: $('.company_all_id').val(),
				page: page1
			},
			success: function(res1) {
				console.log(res1)
				Load(1)
				orderlist = res1.data.list
				$('.bill_use tbody').html(tbodymainf2(orderlist, 1, tddata, addoperation, 'order_id'))
				$('.bill_use .pagination').html(pagechang1(res1.data.count, 10, page1))
				$('.bill_use .laquo').on('click', function() {
					page1 -= 1;
					console.log(page1)
					ajaxuserecord()
				})
				$('.bill_use .pageli').on('click', function() {
					page1 = $(this).html()
					ajaxuserecord()
				})
				//下一页
				$('.bill_use .raquo').on('click', function() {
					page1 += 1;
					ajaxuserecord()
				});
				$('.order').on('click', function() {
					detail($(this).closest('tr').data('id'))
				})
			},
			error:function(){
				Load(1)
				Err('请求失败')
			}
		})
	}

	function ajaxrechargerecord() {
		var tddata = [{
			key: 'company_name',
			type: 1,
			url: ''
		}, {
			key: 'date',
			type: 1,
			class: 'order',
			id: 'order_id'
		}, {
			key: 'type2',
			type: 1,
			state: 'state' //判断数值的正负值
		}, {
			key: 'number',
			type: 1,
			state: 'state' //判断数值的正负值
		}, {
			key: 'state2',
			type: 1,
			state: 'state' //判断数值的正负值
		}];
		var addoperation;
		var index = layer.load(0, {
		  shade: [0.1,'#fff'] //0.1透明度的白色背景
		});
		$.ajax({
			type: "post",
			url: "{:url('unit/finance/topup_list')}",
			data: {},
			success: function(res1) {
				console.log(res1)
				Load(1)
				for(var ii=0;ii<res1.data.length;ii++){
					if(res1.data[ii].type==1){
						res1.data[ii].type2='微信'
                        if(res1.data[ii].state==1){
                            res1.data[ii].state2='未完成'
                        }else if(res1.data[ii].state==3){
                            res1.data[ii].state2='完成'
                        }
					}else if(res1.data[ii].type==2){
						res1.data[ii].type2='预充值'
                        if(res1.data[ii].state==1){
                            res1.data[ii].state2='待审批'
                        }else if(res1.data[ii].state==3){
                            res1.data[ii].state2='通过'
                        }else if(res1.data[ii].state==4){
                            res1.data[ii].state2='不通过'
                        }
					}
				}
				orderlist = res1.data
				$('.bill_topup tbody').html(tbodymainf2(orderlist, 1, tddata, addoperation, 'order_id'))
				$('.bill_topup .pagination').html(pagechang1(res1.data.count, 10, page2))
				$('.bill_topup .laquo').on('click', function() {
					page2 -= 1;
					console.log(page2)
					ajaxrechargerecord()
				})
				$('.bill_topup .pageli').on('click', function() {
					page2 = $(this).html()
					ajaxrechargerecord()
				})
				//下一页
				$('.bill_topup .raquo').on('click', function() {
					page2 += 1;
					ajaxrechargerecord()
				});
//				$('.order').on('click', function() {
//					detail($(this).closest('tr').data('id'))
//				})
			},
			error:function(){
				Load(1)
				Err('请求失败')
			}
		})
	}

	
})